package net.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import net.example.activity.adapter.KeyValuePairAdpter;
import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.model.BasicResult;
import net.example.model.KeyValuePairModel;
import net.example.model.GGUser;
import net.example.ggtalk.R;
import net.example.utils.HeadImgHelper;
import net.example.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class AddFriendOrGroupApplyActivity extends Activity
{
    private List<KeyValuePairModel>  keyValuePairModels;
    private KeyValuePairAdpter keyValuePairAdpter;
    private String currentCatalogName="";
    private EditText et_input;
    private String unitID ="";
    private boolean isGroup=false;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend_apply);
        unitID = getIntent().getStringExtra("friendID");
        isGroup =getIntent().getBooleanExtra("isGroup",false);
        this.initView();
    }

    public void initView()
    {
        TextView headerText= (TextView) findViewById(R.id.headerText);
        headerText.setText("验证申请");
        Button btn_confirm =(Button) findViewById(R.id.btn_confirm);
        btn_confirm.setVisibility(View.VISIBLE);
        btn_confirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String comment=et_input.getText().toString();
                BasicResult result;
                if(isGroup)
                {
                    result = Manager.getInstance().requestAddGroup(unitID,comment);
                }else {
                    boolean isFriendBlackMe = Manager.getInstance().isInHisBlackList(unitID);
                    if (isFriendBlackMe)
                    {
                        ToastUtils.showLong(AddFriendOrGroupApplyActivity.this,"对方拒绝添加您为好友！");
                        return;
                    }
                    result= Manager.getInstance().requestAddFriend(unitID,comment,currentCatalogName);
                }
                if(result==BasicResult.Error)
                {
                    ToastUtils.showLong(AddFriendOrGroupApplyActivity.this,"内部错误！");
                    return;
                }
                ToastUtils.showLong(AddFriendOrGroupApplyActivity.this,"发送成功");
                finish();
            }
        });
        ImageView ct_photo=(ImageView) findViewById(R.id.ct_photo1);
        TextView tv_userName=(TextView) findViewById(R.id.userName);
        et_input=(EditText) findViewById(R.id.et_input);
        et_input.setText("我是"+ClientGlobalCache.getInstance().getCurrentUser().getName());
        et_input.setSelection(et_input.length());
        if(isGroup){
            tv_userName.setText(ClientGlobalCache.getInstance().getGroup(unitID).getName());
            ct_photo.setImageResource(R.drawable.group_ico);
            return;
        }
        GGUser GGUser_friend =ClientGlobalCache.getInstance().getUser(unitID);
        HeadImgHelper.setUserHeadImg(ct_photo, GGUser_friend);
        tv_userName.setText(GGUser_friend.getName());
        ListView listview=(ListView) findViewById(R.id.listview_group);
        keyValuePairModels = new ArrayList<>();
        currentCatalogName=getResources().getString(R.string.friend_catalog_name);
        keyValuePairModels.add(new KeyValuePairModel(getResources().getString(R.string.Catalog), currentCatalogName));
        keyValuePairAdpter = new KeyValuePairAdpter(keyValuePairModels, this);
        listview.setAdapter(keyValuePairAdpter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                KeyValuePairModel model=keyValuePairModels.get(position);
                try
                {
                    if(model.Key.equals(getResources().getString(R.string.Catalog)))
                    {
                        Intent intent = new Intent(AddFriendOrGroupApplyActivity.this, FriendCatalogActivity.class);
                        intent.putExtra(getResources().getString(R.string.Catalog),model.Value);
                        startActivityForResult(intent,0);
                      }
                }catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode != RESULT_OK) {
            return;
        }
        if(requestCode==0)
        {
            currentCatalogName = intent.getExtras().getString(getResources().getString(R.string.Catalog));
            for(KeyValuePairModel model :keyValuePairModels)
            {
                if(model.Key.equals(getResources().getString(R.string.Catalog)))
                {
                    model.Value=currentCatalogName;
                    break;
                }
            }
            keyValuePairAdpter.notifyDataSetChanged();
        }

    }

    public void finish(View view)
    {
        finish();
    }
}
